package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.Tools.CodeMsg;
import com.stylefeng.guns.core.common.Tools.Result;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.modular.system.model.OrgcategoryRoleFunction;
import com.stylefeng.guns.modular.system.service.IOrgcategoryRoleFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
/**
 * 机构分类角色功能控制器 *
 * @author sasazaixian@2018-10-05
 */
@Controller
@RequestMapping(value = {"/gunsApi/auth/orgcategoryRoleFunction"})
@Api(value = "机构分类角色功能api",tags = {"机构分类角色功能操作接口"})
public class OrgcategoryRoleFunctionController extends BaseController {
  @Autowired
  private IOrgcategoryRoleFunctionService orgcategoryRoleFunctionService;

  /**
   * 保存机构分类角色的功能和操作
   * @author sasazaixian@2018-10-05
   */
  @ApiOperation(value = "保存机构分类角色的功能和操作", notes = "保存机构分类角色的功能和操作", httpMethod = "POST")
  @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,consumes= MediaType.APPLICATION_JSON_UTF8_VALUE)
  @BussinessLog(value = "保存机构分类角色的功能和操作")
  @ResponseBody
  public Result add(
          @RequestBody @ApiParam(name = "orgcategoryRoleFunction", value = "sorId:机构分类角色id,objArrys:[{fid:功能id,sm:[操作id]-[操作id],sorid:机构分类id}]")OrgcategoryRoleFunction orgcategoryRoleFunction, BindingResult result
  ) {
    if (result.hasErrors()) {
      return Result.error(CodeMsg.SERVER_EXCEPTION,"参数异常！");
    }
    try{
      orgcategoryRoleFunctionService.insertOrgcategoryRoleFunction(orgcategoryRoleFunction,getToken());
      return Result.success();
    }catch(Exception e){
      return Result.error(CodeMsg.NOT_FIND_DATA, e.getMessage());
    }
  }
  /**
   * 根据机构分类角色查询所拥有的功能和操作
   *  @author sasazaixian@2018-10-05
   */
  @ApiOperation(value = "根据机构分类角色查询所拥有的功能和操作", notes = "根据机构分类角色查询所拥有的功能和操作", httpMethod = "GET")
  @GetMapping(value = "/listTree")
  @ResponseBody
  public Result listTree(@ApiParam(name = "sorId", value = "机构分类角色ID",required = true) @RequestParam(value = "sorId", required = true) String sorId) {
    try{
      return Result.success(orgcategoryRoleFunctionService.selectListOrgcategoryRoleFunction(sorId));
    } catch (Exception e) {
      e.printStackTrace();
      return Result.error(CodeMsg.NOT_FIND_DATA, e.getMessage());
    }
  }
  private String getToken(){
    return getHttpServletRequest().getHeader("Authorization").substring(7);
  }

    
}
